package com.example.jack.community.view.dynamic.dynamic_bean;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.jack.community.R;

import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by DP on 2017/11/20.
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{

    private List<activityData> mdata;
    private Context mContext;
    private LayoutInflater inflater;


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.recyclerview_activity, parent, false);
//        //注册点击事件
        final ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public int getItemCount() {
        return mdata.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView poster;
        TextView id;
        ImageView logo;
        TextView name;
        TextView type;
        TextView time;
        TextView state;
        TextView releasetime;
        View activityView;

        public ViewHolder(View itemView) {
            super(itemView);
            poster = (ImageView) itemView.findViewById(R.id.activity_poster);
            logo = (ImageView) itemView.findViewById(R.id.activity_logo);
            activityView = itemView;
            id = (TextView) itemView.findViewById(R.id.activity_id);
            name = (TextView) itemView.findViewById(R.id.activity_name);
            type = (TextView) itemView.findViewById(R.id.activity_type);
            time = (TextView) itemView.findViewById(R.id.activity_time);
            state = (TextView) itemView.findViewById(R.id.activity_state);
            releasetime = (TextView) itemView.findViewById(R.id.activity_releasetime);
        }
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        activityData data = mdata.get(position);
        Glide.with(mContext).load(data.getPoster()).into(holder.poster);//在设配器器图片请求
        Glide.with(mContext).load(data.getLogo()).into(holder.logo);
        holder.name.setText(data.getName());
        holder.type.setText(data.getType());
        holder.time.setText(data.getTime());
        holder.state.setText(data.getState());
        holder.id.setText(data.getActivityid());
        holder.releasetime.setText(data.getReleasetime());

        //3.在onBindViewHolder方法内，实现回调
        //判断是否设置了监听器
        if (mOnItemClickListener != null) {
            //为ItemView设置监听器
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition(); // 1
                    mOnItemClickListener.onItemClick(holder.itemView, position); // 2
                }
            });
        }

    }

    //重写适配器的变量 Context mContext为了拿到生命周期
    public RecyclerViewAdapter(Context mContext, List<activityData> mdata) {
        this.mContext = mContext;
        this.mdata = mdata;
        inflater = LayoutInflater.from(mContext);
    }



    //实现RecyclerView点击事件

    //1.新建点击内部接口
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    //2.新建私有变量用于保存用户设置的监听器及其set方法
    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
